package com.cardbaobao.cardbabyclient.ui.activity.base;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewStub;
import android.widget.ListView;
import android.widget.TextView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.buried.SelfUtils;
import com.cardbaobao.cardbabyclient.ui.activity.MainActivity;
import com.cardbaobao.cardbabyclient.base.App;
import com.cardbaobao.cardbabyclient.controller.Controller;
import com.cardbaobao.cardbabyclient.ui.dialog.LoadingDialog;
import com.cardbaobao.cardbabyclient.utils.ActivityUtil;
import com.cardbaobao.cardbabyclient.utils.ApplicationHelper;
import com.cardbaobao.cardbabyclient.utils.ToastUtil;
import com.gyf.barlibrary.ImmersionBar;
import com.umeng.analytics.MobclickAgent;

import butterknife.ButterKnife;

/**
 * Activity 基类
 * Created by zad
 * 2016/7/27 9:26
 */

public abstract class BaseActivity extends AppCompatActivity {
    //前一个页面id(即进入到此页面的页面id)
    public static final String PRE_PAGE_ID_S ="pre_page_id";

    protected long mEnterPageTime;
    public String mCurrentPageId;
    public String mPrePageId;

    protected Controller mController;
    protected Context mContext;
    /**
     * 加载动画
     */
    private LoadingDialog mLoadingDialog;

    protected Intent mIntent;

    protected LayoutInflater mInflater;
    /**
     * 没有数据显示的背景
     */
    protected View mViewNotData;
    /**
     * 没有数据时显示
     */
    protected ViewStub mVsNotData;
    /**
     * 没有数据提示内容
     */
    protected TextView mTvNotData;

    protected App mApplication;

    protected Context mApplicationContext;
    /**
     * Activity名称
     */
    private String mActivityName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        ButterKnife.bind(this);
        mContext = this;
        mController = new Controller(mContext);
        mApplication = ApplicationHelper.getApplication();
        mApplicationContext= ApplicationHelper.getApplicationContext();
        mPrePageId=getIntent().getStringExtra(PRE_PAGE_ID_S);
        ImmersionBar.with(this).init();
        initView(savedInstanceState);
        initData(savedInstanceState);
    }

    protected void initView(Bundle savedInstanceState) {
    }

    protected void initData(Bundle savedInstanceState) {
    }

    protected abstract int getLayoutId();

    /**
     * 显示加载动画
     *
     * @param isTouchHide 是否触摸屏幕 动画消失
     */
    public void showProgressDialog(boolean isTouchHide) {
        if (mLoadingDialog == null) {
            mLoadingDialog = new LoadingDialog(this);
        }
        mLoadingDialog.setCanceledOnTouchOutside(isTouchHide);
        mLoadingDialog.setShowText(null);
        if (!isTouchHide) {
            mLoadingDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    mLoadingDialog.dismiss();
                    finish();
                    return true;
                }
            });
        }
        if (!mLoadingDialog.isShowing()) {
            mLoadingDialog.show();
        }
    }

    /**
     * 显示加载动画
     *
     * @param isTouchHide 是否触摸屏幕 动画消失
     * @param showText    加载中的提示语
     */
    public void showProgressDialog(boolean isTouchHide, String showText) {
        if (mLoadingDialog == null) {
            mLoadingDialog = new LoadingDialog(this);
        }
        mLoadingDialog.setShowText(showText);
        mLoadingDialog.setCanceledOnTouchOutside(isTouchHide);
        if (!isTouchHide) {
            mLoadingDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    mLoadingDialog.dismiss();
                    finish();
                    return true;
                }
            });
        }
        if (!mLoadingDialog.isShowing()) {
            mLoadingDialog.show();
        }
    }

    /**
     * 隐藏加载动画
     */
    public void dismissProgressDialog() {
        if (mLoadingDialog != null) {
            mLoadingDialog.dismiss();
        }
    }


    protected void initIntent() {
        if (mIntent == null) {
            mIntent = new Intent();
        }
    }

    protected void initInflater() {
        if (mInflater == null) {
            mInflater = LayoutInflater.from(mContext);
        }
    }

    /**
     * 返回按钮单击事件
     */
    public void goBack(View v) {
        finish();
    }

    /**
     * 返回首页
     */
    public void gotoIndex(View v) {
        initIntent();
        mIntent.setClass(mContext, MainActivity.class);
        mContext.startActivity(mIntent);
        finish();
    }

    /**
     * 设置列表数据是否显示 并提示相关内容
     *
     * @param mListView  要隐藏的列表
     * @param isHaveData 是否有数据
     * @param tipContent 没有数据提示的内容
     */
    protected void setDataShow(ListView mListView, boolean isHaveData, String tipContent) {
        if (mVsNotData == null) {
            mVsNotData = (ViewStub) this.findViewById(R.id.id_vs_not_data);
            if (mVsNotData != null) mVsNotData.inflate();
        }
        if (mViewNotData == null) {
            mViewNotData = this.findViewById(R.id.id_ll_not_data_tip);
        }
        if (mViewNotData != null && mListView != null) {
            if (isHaveData) {//有数据
                mViewNotData.setVisibility(View.GONE);
                mListView.setVisibility(View.VISIBLE);
            } else {//没有数据
                mViewNotData.setVisibility(View.VISIBLE);
                mListView.setVisibility(View.GONE);
                if (!TextUtils.isEmpty(tipContent)) {
                    if (mTvNotData == null) {
                        mTvNotData = (TextView) this.findViewById(R.id.id_tv_no_data_tip);
                    }
                    mTvNotData.setText(tipContent);
                }
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mEnterPageTime=System.currentTimeMillis();
        if (mActivityName == null) {
            mActivityName = ActivityUtil.getRunningActivityName(mContext);
        }
        MobclickAgent.onPageStart(mActivityName);//友盟统计
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mActivityName == null) {
            mActivityName = ActivityUtil.getRunningActivityName(mContext);
        }
        MobclickAgent.onPageEnd(mActivityName);//友盟统计
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(!TextUtils.isEmpty(mCurrentPageId)){
            SelfUtils.buriedBrowseData(mEnterPageTime,
                    mCurrentPageId,
                    mPrePageId);
        }
        ToastUtil.cancelToast();//取消吐司
        ImmersionBar.with(this).destroy();
    }

}
